package com.juzhi.dvrapp.bean;

import com.google.android.gms.maps.model.LatLng;
import com.juzhi.dvrapp.callback.JsonCallback;
import com.juzhi.dvrapp.utils.Config;
import com.juzhi.dvrapp.utils.GreenDaoUtil;
import com.juzhi.dvrapp.utils.MLog;
import com.juzhi.dvrapp.utils.Urls;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.List;

/**
 * Created by YangHu on 2017/8/31.
 */

public class Device implements Serializable {

    //设备状态
    /**
     * -1 ：默认状态   连接超时
     * 1  ：连接中
     * 200：校验通过切设备在线，没有在别处登录
     * 201：访问异常
     * 202：ssid错误
     * 203：用户名密码不一致
     * 204：用户在在别处登录
     * 205：设备不在线
     */
    private int status = Config.DEVICE_RESULT_ERROR_TIMEOUT;
    //设备数据模型
    private DeviceModel deviceModel;
    //设备管理级别
    private int systemLevel;  // 0是admin  1是guest
    //账号id  服务器使用
    private String userId;
    //设备栅栏中心点
    private LatLng latLng;
    //设备栅栏半径
    private int fenceRadius;
    //设备快照
    private String deviceSnap;


    public Device() {

    }

    public Device(int status, DeviceModel deviceModel, int systemLevel, LatLng latLng, int fenceRadius, String deviceSnap) {
        this.status = status;
        this.deviceModel = deviceModel;
        this.systemLevel = systemLevel;
        this.latLng = latLng;
        this.fenceRadius = fenceRadius;
        this.deviceSnap = deviceSnap;
    }

    /**
     * 创建设备对象 连接服务器
     */
    public void createDevice() {
        OkGo.<LzyResponse<DeviceUser>>post(Urls.URL_DEVICELOGIN)
                .tag(this)
                .params(Config.DEVICEID, deviceModel.getSsid())
                .params(Config.USERNAME, deviceModel.getUserName())
                .params(Config.PASSWORD, deviceModel.getPassword())
                .execute(new JsonCallback<LzyResponse<DeviceUser>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<DeviceUser>> response) {
                        status = response.body().status;
                        if (status == Config.DEVICE_RESULT_SUCCESS) {
                            systemLevel = response.body().result.getType();
                            userId = response.body().result.getId();
                            connect();
                        } else {
                            EventBus.getDefault().post(new MessageEven2Refresh(1));
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse<DeviceUser>> response) {
                        super.onError(response);
                        status = Config.DEVICE_RESULT_ERROR_TIMEOUT;
                        EventBus.getDefault().post(new MessageEven2Refresh(1));
                    }
                });


    }

    /**
     * 连接设备 获取设备在线状态
     *
     * @return
     */
    public void connect() {
        OkGo.<HttpGetDeviceInfoResponse>get(Urls.URL_CMS_GET_DEVICE_INFO)
                .tag(this)
                .removeAllParams()
                .params(Config.DEVICE, deviceModel.getSsid())
                .execute(new JsonCallback<HttpGetDeviceInfoResponse>() {
                    @Override
                    public void onSuccess(Response<HttpGetDeviceInfoResponse> response) {
                        HttpGetDeviceInfoResponse.EasyDarwinBean easyDarwin = response.body().getEasyDarwin();
                        if (easyDarwin.getHeader().getErrorNum().equals(Config.RESULT_CMS_SUCCESS)) {
                            status = Config.DEVICE_RESULT_SUCCESS;
                            deviceSnap = easyDarwin.getBody().getSnapURL();
                        } else {
                            status = Config.DEVICE_RESULT_OFFLINE;
                        }
                        EventBus.getDefault().post(new MessageEven2Refresh(1));
                    }

                    @Override
                    public void onError(Response<HttpGetDeviceInfoResponse> response) {
                        super.onError(response);
                        status = Config.DEVICE_RESULT_ERROR_TIMEOUT;
                        EventBus.getDefault().post(new MessageEven2Refresh(1));
                    }
                });
    }

    /**
     * 修改密码
     *
     * @param pwd 密码
     */
    public void alterPwd(String pwd) {
        deviceModel.setPassword(pwd);
        GreenDaoUtil.updateDevice(deviceModel);
    }

    /**
     * 更换用户登录
     *
     * @param userName 账号
     * @param password 密码
     * @return
     */
    public void changeUser(String userName, String password) {
        deviceModel.setUserName(userName);
        deviceModel.setPassword(password);
        GreenDaoUtil.updateDevice(deviceModel);
    }

    public List<MessageModel> getMessage() {
        List<MessageModel> list = GreenDaoUtil.queryMessage2DeviceList(deviceModel.getSsid());
        if (list.size() == 0) {
            list.add(new MessageModel(null, getDeviceModel().getDeviceName(), getDeviceModel().getSsid(), System.currentTimeMillis() + "", 1, 1200, 1200));
            list.add(new MessageModel(null, getDeviceModel().getDeviceName(), getDeviceModel().getSsid(), System.currentTimeMillis() + "", 1, 1200, 1200));
            list.add(new MessageModel(null, getDeviceModel().getDeviceName(), getDeviceModel().getSsid(), System.currentTimeMillis() + "", 1, 1200, 1200));
            list.add(new MessageModel(null, getDeviceModel().getDeviceName(), getDeviceModel().getSsid(), System.currentTimeMillis() + "", 1, 1200, 1200));
            list.add(new MessageModel(null, getDeviceModel().getDeviceName(), getDeviceModel().getSsid(), System.currentTimeMillis() + "", 1, 1200, 1200));
            list.add(new MessageModel(null, getDeviceModel().getDeviceName(), getDeviceModel().getSsid(), System.currentTimeMillis() + "", 1, 1200, 1200));
            list.add(new MessageModel(null, getDeviceModel().getDeviceName(), getDeviceModel().getSsid(), System.currentTimeMillis() + "", 1, 1200, 1200));
            list.add(new MessageModel(null, getDeviceModel().getDeviceName(), getDeviceModel().getSsid(), System.currentTimeMillis() + "", 1, 1200, 1200));
            list.add(new MessageModel(null, getDeviceModel().getDeviceName(), getDeviceModel().getSsid(), System.currentTimeMillis() + "", 1, 1200, 1200));
            list.add(new MessageModel(null, getDeviceModel().getDeviceName(), getDeviceModel().getSsid(), System.currentTimeMillis() + "", 1, 1200, 1200));
        }
        MLog.w(list.size());
        return list;
    }


    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public DeviceModel getDeviceModel() {
        return deviceModel;
    }

    public Device setDeviceModel(DeviceModel deviceModel) {
        this.deviceModel = deviceModel;
        return null;
    }

    public int getSystemLevel() {
        return systemLevel;
    }

    public void setSystemLevel(int systemLevel) {
        this.systemLevel = systemLevel;
    }

    public LatLng getLatLng() {
        return latLng;
    }

    public void setLatLng(LatLng latLng) {
        this.latLng = latLng;
    }

    public int getFenceRadius() {
        return fenceRadius;
    }

    public void setFenceRadius(int fenceRadius) {
        this.fenceRadius = fenceRadius;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getDeviceSnap() {
        return deviceSnap;
    }

    public void setDeviceSnap(String deviceSnap) {
        this.deviceSnap = deviceSnap;
    }


}
